<?
session_start();
include 'php/connect.php';
include 'php/functions.php';

$sId =  mysql_real_escape_string($_POST['specId']);
$amount =  mysql_real_escape_string($_POST['amount']);



if (isset($_SESSION['userId'])){
	$canWrite = true;
}else{
	$canWrite = false;
}

if (isset($_POST['specId']) && isset($_POST['amount'])){

	$output = "
				<script type='text/javascript'>
					$(function(){
						$('.ratings').children().not(':radio').hide();
						$('.ratings').stars({
							disabled: true
						});
					});
					
					$(document).ready(function(){
						$('.delete-comment').click(function(e) {
							var clicked = jQuery(e.target);
							var id = clicked.attr('id')
							deleteComment(id, '".$sId."');
						});
						$('.more-comment').click(function(e) {
							loadSpecComments(".$sId.",".($amount+5).");
						});
					});
				</script>
				<table id='me_spec_comment' class='content-subtitle-table'>";
				
					
				$abfrage = "SELECT * FROM (
					   SELECT comm.commentId as id
					   ,comm.specId
					   ,comm.comment
					   ,comm.cdate as c_cdate
					   ,comm.udate as c_udate
					   ,spec.public
					   ,user.userName
					   ,user.avatar
					   ,user.posts
					   ,user.cdate as u_cdate
					   ,rate.value
					   ,'COMM' as type 
				FROM (((
				ffxivc_comments as comm 
				LEFT JOIN 
				ffxivc_specs as spec
				ON comm.specId = spec.specId)
				INNER JOIN ffxivc_users as user
				ON user.userId = comm.userId)
				LEFT JOIN ffxivc_ratings as rate
				ON user.userId = rate.userId
				AND spec.specId = rate.specId)			
				
				UNION 
				
				SELECT rateId as id
					,rate.specId
					,(CASE WHEN rate.cdate=rate.udate THEN 'rated this spec with' ELSE 'updated his rating to' END) as comment
					,rate.cdate as c_cdate
					,rate.udate as c_udate
					,spec.public
					,user.userName
					,user.avatar
					,user.posts
					,user.cdate as u_cdate
					,rate.value
					,'RATE' as type
					
				FROM ((ffxivc_ratings as rate
				LEFT JOIN 
				ffxivc_specs as spec
				ON rate.specId = spec.specId)
				INNER JOIN ffxivc_users as user
				ON user.userId = rate.userId)
				
				) as A";
				
				if(!$canWrite){
					$abfrage .= " WHERE A.specId = \"".$sId."\" AND A.public = 'Y'";
				}else{
					$abfrage .= " WHERE A.specId = \"".$sId."\"";
				}
				$abfrage .= " ORDER BY A.c_udate DESC";

				$res = mysql_query($abfrage);
				$num = mysql_num_rows($res);	
				$i = 1;

				//TODO CHANGE QUERY!!!!
				$abfrageSpec = "SELECT spec.description, user.userName, spec.cdate, spec.udate, user.avatar 
								FROM (
								ffxivc_specs as spec
									LEFT JOIN
								ffxivc_users as user
								ON spec.userId = user.userId
								) 
								WHERE specId = \"".$sId."\"";
				$resSpec = mysql_query($abfrageSpec);
				$desc = mysql_result($resSpec, 0, "description");
				$av = mysql_result($resSpec, 0, "avatar");
				$usr = mysql_result($resSpec, 0, "userName");
				$crea = mysql_result($resSpec, 0, "cdate");
				$up = mysql_result($resSpec, 0, "udate");
				
				if($av != ""){
						$specAvatar = "<div class='border-av'><img class='avatar-pic' src='".$av."?".time()."'/></div>";
					}else{
						$specAvatar = "<div class='border-av'><img class='avatar-pic' src='media/no-avatar.png'/></div>";
				}
				$output .= "
					<tr class='spec-row tr-table border-bottom'>
						<td class='td-table ver-top cur-point' style='padding-left: 10px; padding-right: 10px;'>
							<a class='cur-point' name='no' href=\"javascript:getUser('".$usr."');\">".$specAvatar."</a>
						</td>
						<td class='td-table ver-top pad-t-12'>
							<div style='height:15px;'>
								<a class='cur-point fl-left' name='no' href=\"javascript:getUser('".$usr."');\">".$usr."</a>";
				if($crea == $up){
					$output .= "<div class='normal-text bold-text fl-left'>created this spec";
				}else{
					$output .= "<div class='normal-text bold-text fl-left'>updated his spec";
				}
								
				if($desc != ""){
					$output .= " describing it as:</div>
								<div class='spec-comment normal-text pad-5 fl-left' style='overflow: hidden;white-space: pre-wrap;'>".$desc."</div>";
					
				}else{
					$output .= "</div>";
				}
				$output .= "</div>
							<div class='normal-text pad-5 font-10 fl-left'>
								<div class='op-4 fl-left' style='padding-bottom:5px; padding-right:5px;'>".$up."</div>
							</div>
							</div>
						</td>
					</tr>";
				
				if($canWrite){
					if($_SESSION['avatar'] != "" && $_SESSION['avatar'] != NULL){
						$MyAvatar = $_SESSION['avatar'];
					}else{
						$MyAvatar = "media/no-avatar.png";
					}
					$output .= "
					<tr class='spec-row tr-table border-bottom spec-detail-list-selected'>
						<td class='td-table ver-top cur-point' style='padding-right: 10px; padding-left: 10px;'>
							<a class='cur-point' name='no' href=\"javascript:getUser('".$_SESSION['userName']."');\"><div class='border-av'><img class='avatar-pic' src='".$MyAvatar."?".time()."'/></div></a>
						</td>
						<td id='spec-comment-write-container' class='td-table ver-top pad-t-12' style='padding-right: 22px; padding-left: 5px; padding-top: 15px;'>
							<textarea id='write-comment' type='text'>Comment on ".$usr."'s spec...</textarea>
						</td>
					</tr>
					<script>
						var shiftPressed = false;
						$(window).keydown(function(evt) {
						  if (evt.which == 16) { // shift
							shiftPressed = true;
						  }
						}).keyup(function(evt) {
						  if (evt.which == 16) { // shift
							shiftPressed = false;
						  }
						});
						$('#write-comment').keypress(function(event) {
							if (event.which == 13 && !(shiftPressed)){
								writeComment(".$sId.", $(this).val())
								event.preventDefault();
							}
						});

						$('#write-comment').focus(function(){
							$(this).val('');
						});
					</script>";
				}else{
					$output .= "
					<tr class='spec-row tr-table border-bottom spec-detail-list-selected'>
						<td class='td-table ver-top cur-point' style='padding-right: 10px; padding-left: 10px;'>
							<a class='' name='no' ><div class='border-av'><img class='avatar-pic' src='media/no-avatar.png'/></div></a>
						</td>
						<td id='spec-comment-write-container' class='td-table ver-top pad-t-12' style='padding-right: 22px; padding-left: 5px; padding-top: 15px;'>
							<textarea id='write-comment' readonly type='text'>Login to write a comment...</textarea>
						</td>
					</tr>";
				}
				
				
				while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
					if($row['avatar'] != ""){
						$MyAvatar = "<div class='border-av'><img class='avatar-pic' src='".$row['avatar']."?".time()."'/></div>";
					}else{
						$MyAvatar = "<div class='border-av'><img class='avatar-pic' src='media/no-avatar.png'/></div>";
					}
					$output .= "<tr id='tr_".$row['comId']."' class='spec-row tr-table ";
					if($i != $amount && $i!=$num){
						$output .= "border-bottom";
					}
					$output .= "' name='".$row['comId']."'>
						<td class='td-table ver-top cur-point' style='padding-left: 10px; padding-right: 10px;'>
							<a class='cur-point' name='no' href=\"javascript:getUser('".$row['userName']."');\">".$MyAvatar."</a>
						</td>
						<td class='td-table ver-top pad-t-12'>
							<div style='height:15px;'>
								<a class='cur-point fl-left' name='no' href=\"javascript:getUser('".$row['userName']."');\">".$row['userName']."</a>";
							
					/*if($row['value'] != null){
										
						$output .= 	"
								<div class='fl-right'>
									<!--<div class='fl-left normal-text bold-text blue-text font-10' style='padding-right:10px;'>Rated this spec: </div>-->
									<div class='fl-right'>
										<form class='ratings' action='' method='post'>";
										
										foreach (get_options() as $id => $title){
											$output .= 	"<input type='radio' name='rate' value='".$id."' id='rate".$id."'";
											if($id==$row['value']){
												$output .= 	" checked";
											}
											$output .= 	" />";
										}
										
							$output .= 	"
										</form>
									</div>
								</div>";
					}*/
					
					if($row['type'] == "RATE"){
						$output .= "
							<div class='normal-text bold-text fl-left' style='overflow: hidden;'>".$row['comment']."</div>
							<div class='fl-left' style='padding-left:5px; padding-top: 1px; width: 80px;'> 
								<form class='ratings' action='' method='post'>";
										
						foreach (get_options() as $id => $title){
							$output .= 	"<input type='radio' name='rate' value='".$id."' id='rate".$id."'";
							if($id==$row['value']){
								$output .= 	" checked";
							}
							$output .= 	" />";
						}
						
						$output .= "	</form>
							</div><div style='clear:both;'/>";
					}else{
					
						$output .= "
								</div>
								<div class='spec-comment normal-text pad-5' style='overflow: hidden;white-space: pre-wrap;'>".$row['comment']."</div>";
					}
							
					$output .= "	<div class='normal-text pad-5 font-10 fl-left'>
								<div class='op-4 fl-left' style='padding-bottom:5px; padding-right:5px;'>".$row['c_udate']."</div>
							";
								
					
					if($_SESSION['userName'] == $row['userName'] && $row['type'] != "RATE"){
						$output .= 	"<div name='no' class='normal-text bold-text grey-text pad-lr-5 fl-left ' style='padding-top:2px;'>*</div>
								<a id='".$row['id']."' class='delete-comment fl-left pad-lr-5 font-10' style='cursor: pointer;' name='no' >Delete</a>";
					}
					
					$output .= "</div>
							</div>
						</td>
					</tr>";
					if($i == $amount){
						break;
					}
					$i++;
				}
				
				if($i<$num){
					// MORE COMMENTS TO SHOW
					$output .= "<tr>
						<td  colspan='2' style='text-align: center;'>
							<div class='more-comment center cur-point arrow-down' style='display:inline;'/>
						</td>
					</tr>";
					
				}

				mysql_free_result($res);
			
		$output .= "</table><div style='clear:both;'>";

	echo  $output;
}

?>